<?php

namespace app\api\controller;

class Index extends Common {
    /**
     * 书店小程序商城首页
     * @return [json]
     */
    public function index(){
        $dataurl = $this->__DATAURL__;
        // 如果缓存首页没有数据，那么就读取数据库
        // 获取首页顶部轮播图
        $ggtop = db('ads')->field('ads_id, ads_name, ads_photo')->order('ads_sort desc, ads_id asc')->limit(5)->select();
        foreach ($ggtop as $k => $v){
            $ggtop[$k]['ads_photo'] = $dataurl.$v['ads_photo'];
            $ggtop[$k]['ads_name'] = urldecode($v['ads_name']);
        }

        // 首页推荐品牌20个
        $brand = db('brand')->where('1=1')->field('brand_id, brand_name, brand_photo')->limit(20)->select();
        foreach ($brand as $k => $v){
            $brand[$k]['brand_photo'] = $dataurl.$v['brand_photo'];
        }

        // 首页推荐产品
        $pro_list = db('product')->where('product_del = 0 AND product_pro_type = 1 AND product_is_down = 0 AND product_type = 1')
                                 ->order('product_sort desc, product_id desc')
                                 ->field('product_id, product_name, product_intro, product_photo_x, product_price_yh, product_price, product_shiyong')
                                 ->limit(8)->select();
        foreach($pro_list as $k => $v){
            $pro_list[$k]['product_photo'] = $dataurl.$v['product_photo'];
        }

        $this->return_msg(1, '请求成功！', array('ggtop' => $ggtop, 'brand' => $brand, 'proList' => $pro_list));
    }


}